package com.ydgg.ltar.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ydgg.ltar.api.ApiResult;
import com.ydgg.ltar.mapper.AreaGiftMapper;
import com.ydgg.ltar.mapper.AreaMapper;
import com.ydgg.ltar.model.Area;
import com.ydgg.ltar.model.AreaExample;
import com.ydgg.ltar.model.AreaGift;
import com.ydgg.ltar.util.PageInfo;
import com.ydgg.ltar.util.Result;


/**
 * 区域
 * 作者： 张叶
 * 创建时间：2019年3月11日  下午3:46:17
 */	
@Service
public class AreaService {

	@Autowired
	private AreaMapper areaMapper;
	
	@Autowired
	private AreaGiftMapper areaGiftMapper;

	
	/**
	 * 分享
	 * 作者： 张叶
	 * 创建时间：2019年3月28日  下午2:46:49
	 */
	public ApiResult share(Long id){
		ApiResult result = new ApiResult();
		if(id != null && !"".equals(id)){
			AreaGift af = areaGiftMapper.selectByPrimaryKey(id);
			if(af != null && !"".equals(af)){
				Long aId = af.getAreaId();
				if(aId != null && !"".equals(aId)){
					Area a = areaMapper.selectByPrimaryKey(af.getAreaId());
					result.data.put("name", a.getName());
					result.data.put("link", af.getLink());
					result.data.put("image", af.getImageUrl());
					result.data.put("info", af.getInfo());
				}else{
					result.data.put("name", af.getName());
					result.data.put("link", af.getLink());
					result.data.put("image", af.getImageUrl());
					result.data.put("info", af.getInfo());
				}
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "查无结果";
			}
		}else{
			result.msg.status = 3;
			result.msg.desc = "参数不能为空";
		}
		return result;
	}
	
	
	/**
	 * 列表
	 * 作者： 张叶
	 * 创建时间：2019年3月14日  上午10:54:45
	 */
	public Map<String, Object> selectByPage(PageInfo pageInfo,Integer rows, Integer page,
			String sidx, String sord, String name) {
		
		if (pageInfo == null) {
			pageInfo = new PageInfo();
		}
		if (pageInfo.getPage() < 2
				|| pageInfo.getPage() >= pageInfo.getTotalPage()) {
			pageInfo.setCount(0);
		}
		if (rows != null) {// 每页显示条数
			pageInfo.setPageSize(rows);
		}
		if (page != null) {// 当前页
			pageInfo.setPage(page);
		}

		// 排序
		String order = " order by created_at desc";
		if (sidx != null && sidx != "") {// 排序字段
			order = " order by " + sidx + " ";
			if (sord != null && sord != "") {// 排序
				order += sord;
			}
		}
		String where = "1 = 1 and status != 'deleted'";

		if(name != null && !"".equals(name)){
			where += " and name like '%" + name + "%'";
		}

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		List<Map<String, Object>> m = areaMapper.selectByPage(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(int i = 0 ; i < m.size() ; i ++){
			Date createTime = (Date) m.get(i).get("created_at");
			m.get(i).put("created_at", sdf.format(createTime));
		}
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("total", pageInfo.getTotalPage());
		data.put("list", m);
		return data;
	}
	

	
	
	public Result insert(Area a){
		Result result = new Result();
		a.setCreatedAt(new Date());
		a.setStatus("on");
		int i = areaMapper.insert(a);
		if(i > 0){
			result.code = 0;
			result.message = "添加成功";
		}else{
			result.code = 1;
			result.message = "系统错误，添加失败";
		}
		return result;
	}
	
	
	/**
	 * 删除
	 * 作者： 张叶
	 * 创建时间：2019年3月14日  上午10:53:26
	 */
	@Transactional
	public Result deleted(Long id){
		Result result = new Result();
		Area a = areaMapper.selectByPrimaryKey(id);
		if(a != null){
			a.setStatus("deleted");
			int i = areaMapper.updateByPrimaryKeySelective(a);
			if(i > 0){
				result.code = 0;
				result.message = "添加成功";
			}else{
				result.code = 1;
				result.message = "系统错误，添加失败";
			}
		}else{
			result.code = 0;
			result.message = "删除成功";
		}
		return result;
	}
	
	
	public List<Area> selectByWhere(String where){
		List<Area> list = areaMapper.selectByWhere(where);
		return list;
	}
	
	public Area selectByPrimaryKey(Long id){
		Area a = areaMapper.selectByPrimaryKey(id);
		return a;
	}
	
	public Result updateBySelective(Area a){
		Result result = new Result();
		int i = areaMapper.updateByPrimaryKeySelective(a);
		if( i > 0){
			result.code = 0;
			result.message = "修改成功";
		}else{
			result.code = 1;
			result.message = "系统错误，修改失败";
		}
		return result;
	}
	
	
	public List<Area> selectByExample(AreaExample example){
		List<Area> list = areaMapper.selectByExample(example);
		return list;
	}
}
